package cpu.footprint.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;
import java.util.Map;

import cpu.footprint.R;
import cpu.footprint.api.AppXUtilDao;
import cpu.footprint.api.MyApplication;
import cpu.footprint.api.ResultMessage;
import cpu.footprint.bean.User;
import cpu.footprint.common.BaseActivity;
import cpu.footprint.util.CreateDialogUtil;

/**
 * Created by Administrator on 2017/3/27.
 */
@ContentView(R.layout.activity_serachgroup)
public class GroupSearchActivty extends BaseActivity {
    @ViewInject(R.id.title_content)
    private TextView title_content;//title内容
    @ViewInject(R.id.title_menu_text)
    private TextView title_menu_text;//title功能
    @ViewInject(R.id.title_menu_image)
    private ImageView title_menu_image;//title功能
    @ViewInject(R.id.title_menu_image2)
    public ImageView title_menu_image2;//标题栏图形功能
    @ViewInject(R.id.edit_serachgroupID)
    private EditText edit_serachgroupID;//搜索框
    private Context mContext;
    private User user;
    private AppXUtilDao mAppXUtilDao = new AppXUtilDao();
    private MyApplication myApplication;
    private Dialog mDialog;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (mDialog != null) {
                if (mDialog.isShowing())
                    mDialog.dismiss();
            }
            switch (msg.what) {
                case ResultMessage.GET_GROUP_SUCCESS:
                    String jsonStr=msg.obj.toString();
                    CreateDialogUtil.ToastShow(mContext, jsonStr);
                    break;
                case ResultMessage.GET_GROUP_FAILED:
                    String jsonString=msg.obj.toString();
                    CreateDialogUtil.ToastShow(mContext, jsonString);
                    break;
                case ResultMessage.FAILED:
                    CreateDialogUtil.ToastShow(mContext, "连接失败");
                    break;
                case ResultMessage.TIMEOUT:
                    CreateDialogUtil.ToastShow(mContext, "连接超时");
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        myApplication= (MyApplication) this.getApplication();
        user=myApplication.getUser();
        mContext=GroupSearchActivty.this;
    }
    private void initView() {
        title_content.setText("搜索群组");
        title_menu_image.setVisibility(View.GONE);
        title_menu_text.setVisibility(View.GONE);
        title_menu_image.setVisibility(View.GONE);
        title_menu_image2.setVisibility(View.GONE);
        title_menu_image2.setImageResource(R.mipmap.groupinfo);
        mDialog= CreateDialogUtil.CreateDialog(GroupSearchActivty.this,"加载中...",mHandler);
    }
    @Event(value = {R.id.title_back,R.id.btn_serachGroup})
    private void OnClick(View v){
        switch (v.getId()){
            case R.id.title_back: {
                GroupSearchActivty.this.finish();
                overridePendingTransition(0,R.anim.fade);
            }
            break;
            case R.id.btn_serachGroup: {
                String id=edit_serachgroupID.getText().toString().trim();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("mobilePhone", user.getMobilePhone());
                map.put("token", user.getToken());
                map.put("id", id);
                mAppXUtilDao.GetGroupById(mHandler,map);
                mDialog.show();
            }
            break;
        }
    }
}
